const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : `0${n}`
}

function parseTimer(time) {
    let now = new Date(time);
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    let date = now.getDate();
    let hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();
    let minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
    let second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();
    return year + "-" + month + "-" + date;
    // return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

function queryUrl(url) {
    const querys = url.slice(url.indexOf("?") + 1)
    let obj = {}
    const items = querys.split("&")
    items.map(item => {
        const [key, value] = item.split("=")
        obj[key] = value
    })
    return obj;
}

module.exports = {
    formatTime,
    parseTimer,
    formatNumber,
    queryUrl
}